package com.longrise.android.byjk.plugins.tabthird;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.HraPaymentEvent;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.hra.hratabsecond.MineOrderDetailContract;
import com.longrise.android.byjk.plugins.hra.hratabsecond.MineOrderDetailPresenter;
import com.longrise.android.byjk.plugins.hra.pay.HraPaymentActivity;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2018/1/14.
 *我的订单详情
 */

public class MineOrderDetailActivity extends BaseActivity2<MineOrderDetailPresenter> implements MineOrderDetailContract.View,View.OnClickListener {

    public static final String ORDERDETAILID = "orderdetailid";
    public static final String ORDERDETAILSTATE = "orderdetailstate";
    private TextView mOrderState;
    private TextView mOrderPrice;
    private RelativeLayout rl_order_detail_notice;
    private TextView mBookTime;
    private TextView mOrderPoint;
    private TextView mOrderArea;
    private TextView mOrdertel;
    private TextView mOrderType;
    private TextView mOrderTime;
    private TextView mOrderNum;
    private MineOrderBean mineOrderBean;
    private ScrollView sc_order_detail;
    private LinearLayout pend_pay_header;
    private LinearLayout order_pay_header;
    private LinearLayout rl_order_pend_pay;
    private ImageView iv_back;
    private TextView tv_time_left;
    private LinearLayout ll_order_timeout;
    private Button btn_pend_pay;
    private String orderId;
    private TextView mOrderName;
    private TextView mOrderSex;
    private TextView mOrderCard;
    private TextView mOrderPhone;
    private Long deadline;
    private TextView tv_order_detail_count;
    private Dialog mEndorseDialog;
    private TextView tv_pay_price;
    private TextView tv_pend_payline;
    private String deadtime;
    private String mPrice;
    private int mTradeState;
    private CountDownTimer mTimer;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_mine_order_detail;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        pend_pay_header = (LinearLayout) findViewById(R.id.pend_pay_header);
        order_pay_header = (LinearLayout) findViewById(R.id.order_pay_header);
        rl_order_pend_pay = (LinearLayout) findViewById(R.id.rl_order_pend_pay);
        iv_back = (ImageView) findViewById(R.id.iv_order_detail_back);
        tv_order_detail_count = (TextView) findViewById(R.id.tv_order_detail_count);

        ll_order_timeout = (LinearLayout) findViewById(R.id.ll_order_timeout);
        rl_order_detail_notice = (RelativeLayout) findViewById(R.id.rl_order_detail_notice);
        mOrderState = (TextView) findViewById(R.id.tv_order_state);
        mOrderPrice = (TextView) findViewById(R.id.tv_order_detail_price);
        mBookTime = (TextView) findViewById(R.id.tv_book_time);
        mOrderPoint = (TextView) findViewById(R.id.tv_order_detail_point);
        mOrderArea = (TextView) findViewById(R.id.tv_order_detail_area);
        mOrdertel = (TextView) findViewById(R.id.tv_order_detail_tel);
        mOrderType = (TextView) findViewById(R.id.tv_order_detail_type);
        mOrderTime = (TextView) findViewById(R.id.tv_order_detail_time);
        mOrderNum = (TextView) findViewById(R.id.tv_order_detail_num);
        sc_order_detail = (ScrollView) findViewById(R.id.sc_order_detail);
        tv_time_left = (TextView) findViewById(R.id.tv_time_left);
        btn_pend_pay = (Button) findViewById(R.id.btn_pend_pay);
        mOrderName = (TextView) findViewById(R.id.tv_order_detail_name);
        mOrderSex = (TextView) findViewById(R.id.tv_order_detail_sex);
        mOrderCard = (TextView) findViewById(R.id.tv_order_detail_card);
        mOrderPhone = (TextView) findViewById(R.id.tv_order_detail_phone);
        tv_pay_price = (TextView) findViewById(R.id.tv_pay_price);
        tv_pend_payline = (TextView) findViewById(R.id.tv_pend_payline);
        getExtra();
        initEvent();
        showNormalLoadingPage();
        mPresenter.getOrderDetailData(orderId);
    }


    public void initEvent(){
        btn_pend_pay.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        rl_order_detail_notice.setOnClickListener(this);
    }


    public void getExtra(){
        Intent intent = getIntent();
        orderId = intent.getStringExtra(ORDERDETAILID);
    }

    public void setData2View(EntityBean bean){
        sc_order_detail.setVisibility(View.VISIBLE);
        String storename = bean.getString("storename");
        String address = bean.getString("address");
        String storephone = bean.getString("storephone");
        String examinedate = bean.getString("examinedate");
        String timeslot = bean.getString("timeslot");
        String username = bean.getString("username");
        String sex = bean.getString("sex");
        String cardno = bean.getString("cardno");
        String usermobile = bean.getString("usermobile");
        String orderid = bean.getString("orderid");
        mPrice = bean.getString("total_fee");
        String machinemodel = bean.getString("machinemodel");
        String createtime = bean.getString("createtime");
        deadtime = bean.getString("deadtime");
        mTradeState = bean.getInt("trade_state");

        //已付款
        if(1 == mTradeState){
            int endorse = bean.getInt("endorse");
            setToolbarVisible(true);
            setToolbarTitle(AppUtil.getString(R.string.book_detail));
            order_pay_header.setVisibility(View.VISIBLE);
            pend_pay_header.setVisibility(View.GONE);
            ll_order_timeout.setVisibility(View.GONE);
            rl_order_pend_pay.setVisibility(View.GONE);
            if(0 == endorse){
                rl_order_detail_notice.setVisibility(View.GONE);
            }else {
                rl_order_detail_notice.setVisibility(View.VISIBLE);
                tv_order_detail_count.setText("该订单已经改签" + endorse + "次");
            }
            mOrderState.setText("已付款");
            mBookTime.setText("下单时间: "+createtime); //下单时间
        }else if(-1 == mTradeState){     //已取消
            setToolbarVisible(true);
            setToolbarTitle(AppUtil.getString(R.string.book_detail));
            order_pay_header.setVisibility(View.VISIBLE);
            pend_pay_header.setVisibility(View.GONE);
            ll_order_timeout.setVisibility(View.VISIBLE);
            rl_order_pend_pay.setVisibility(View.GONE);
            rl_order_detail_notice.setVisibility(View.GONE);
            mOrderState.setText("已超时");
            mBookTime.setText("下单时间: "+createtime); //下单时间
        }else if(0 == mTradeState){    //未付款
            deadline = bean.getLong("deadline");
            setToolbarVisible(false);
            order_pay_header.setVisibility(View.GONE);
            pend_pay_header.setVisibility(View.VISIBLE);
            rl_order_pend_pay.setVisibility(View.VISIBLE);
            ll_order_timeout.setVisibility(View.GONE);
            rl_order_detail_notice.setVisibility(View.GONE);
            tv_pay_price.setText("¥" + mPrice);
            tv_pend_payline.setText("订单需要在"+ deadtime +"前支付，超时将自动关闭");
            handleTimer();
        }

        mOrderPoint.setText(storename);
        mOrderArea.setText(address);
        mOrdertel.setText(storephone);
        mOrderType.setText(machinemodel);
        mOrderPrice.setText("¥" + mPrice);
        mOrderTime.setText(examinedate +" " + timeslot);
        mOrderNum.setText(orderid);
        mOrderName.setText(username);
        mOrderSex.setText(sex);

        mOrderCard.setText(Html.fromHtml("身份证&#160&#160<font color='#333333'>" + cardno + "</font>"));
        mOrderPhone.setText(Html.fromHtml("手机号&#160&#160<font color='#333333'>" + usermobile + "</font>"));
    }

//    @Override
//    public void setStatusBarColor() {
//        super.setStatusBarColor();
//        AppUtil.setStatusBarColor(this, Color.parseColor("#FF1686CC"));
//    }

    //计时器
    public void handleTimer(){

        /** 倒计时60秒，一次1秒 */
        mTimer = new CountDownTimer(deadline, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                int time = (int) (millisUntilFinished/1000);
                int minute = time/60;
                int second = time%60;
                if (minute == 0) {
                    if (second == 0) {
//                        tv_time_left.setText("Time out !");
                    } else {
                        second--;
                        if (second >= 10) {
                            tv_time_left.setText("00:" + "0" + minute + ":" + second);
                        } else {
                            tv_time_left.setText("00:" + "0" + minute + ":0" + second);
                        }
                    }
                } else {
                    if (second == 0) {
                        second = 59;
                        minute--;
                        if (minute >= 10) {
                            tv_time_left.setText("00:" + minute + ":" + second);
                        } else {
                            tv_time_left.setText("00:" + "0" + minute + ":" + second);
                        }
                    } else {
                        second--;
                        if (second >= 10) {
                            if (minute >= 10) {
                                tv_time_left.setText("00:" + minute + ":" + second);
                            } else {
                                tv_time_left.setText("00:" + "0" + minute + ":" + second);
                            }
                        } else {
                            if (minute >= 10) {
                                tv_time_left.setText("00:" + minute + ":0" + second);
                            } else {
                                tv_time_left.setText("00:" + "0" + minute + ":0" + second);
                            }
                        }
                    }
                }
//                tv.setText("还剩"+millisUntilFinished/1000+"秒");
            }

            @Override
            public void onFinish() {
                setToolbarVisible(true);
                setToolbarTitle(AppUtil.getString(R.string.book_detail));
                order_pay_header.setVisibility(View.VISIBLE);
                pend_pay_header.setVisibility(View.GONE);
                ll_order_timeout.setVisibility(View.VISIBLE);
                rl_order_pend_pay.setVisibility(View.GONE);
                rl_order_detail_notice.setVisibility(View.GONE);
                mOrderState.setText("已超时");
//                mBookTime.setText(createtime); //下单时间
            }
        }.start();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击返回按钮
            case R.id.iv_order_detail_back:
                finish();
                break;

            //去付款
            case R.id.btn_pend_pay:
                toPay();
                break;

            //点击该订单已改签几次
            case R.id.rl_order_detail_notice:
                showEndorseDialog();
                break;

        }
    }

    //显示改签规则的dialog
    public  void showEndorseDialog() {
        View mContentDialog = View.inflate(mContext, R.layout.dialog_endorse_notice, null);
        TextView tv_confirm= (TextView) mContentDialog.findViewById(R.id.dialog_tv_confirm);
        mEndorseDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mContentDialog, 280, 180);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });
    }

    @Override
    public void refreshData(EntityBean bean) {
        EntityBean result = bean.getBean("result");

        setData2View(result);
    }

    @Override
    public void setRefreshing(boolean isRefreshing) {

    }

    /**
     * 去付款
     */
    private void toPay(){
        String payname = UserInfor.getInstance().getUserNickName();
        String paycardno = UserInfor.getInstance().getUsersfzh();
        HraPaymentActivity.toPushActivity(this,orderId,payname,paycardno,"1",mPrice);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registPayResult(HraPaymentEvent event){
        if(event.isClose()){ //课程已经购买成功 刷新页面数据
            mPresenter.getOrderDetailData(orderId);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if(mTimer != null){
            mTimer.cancel();
            mTimer.onFinish();
        }

    }
}
